import Vue from 'vue'
import Vuex from 'vuex'
import userModel from "../api/user.js"
Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    token: localStorage.getItem("token") ? localStorage.getItem('token') : ""
  },
  mutations: {
    setToken(state,token){
      state.token = token;
      localStorage.setItem("token",token)
    }
  },
  actions: {
     login({commit},form) {
      // console.log(form);
      return new Promise( async (resolve,reject) =>{
      const {username,password} = form
      const res = await userModel.userLogin(username, password);
      console.log(res);
    
      if(res == "用户名或密码错误"){
        return
        
        }
         resolve(res)
      commit("setToken",res.token)
      })
 
    }
  },
  modules: {
  }
})
